/*
 * HardDisk.h
 *
 *  Created on: Jul 11, 2009
 *      Author: avishai
 */

#ifndef HARDDISK_H_
#define HARDDISK_H_
#include <iostream>
#include <list>
using namespace std;
#include <fstream>
#include "DATA.h"

class HardDisk {
public:
	HardDisk(char* fileName);
	virtual ~HardDisk();

	/*allocates a new i-node and returns its number	 */
	int allocateInode();

	/*
	 * frees the specified i-node
	 * and also frees all the data blocks pointed to by it
	 * (directly or indirectly).
	 */
	void freeInode(int i_node);

	/*allocates a data block and returns its number	 */
	int allocateDataBlock();

	/*
	 * frees the specified block.
	 *Use (and update) the maps in the super
	 *block in these allocate/free methods.
	 */
	void freeDataBlock(int dblock);

	/*return the type of the file (normal/directory/soft-link)	 */
	int getInodeType(int i_node);

	/* set the type of the file (normal/directory/soft-link).	 */
	void setInodeType(int i_node, int filetype);

	/*
	 *return the number of the i-th data block containing the data of this file
	 *(for 0<=i<=9 this is just stored in the 10 pointers in this i_node, for i>9,
	 * you must retrieve it from the indirect data block..).
	 */
	int getDataBlock(int i_node, int i);

	/* sets the i-th data block of this file to be dblockNum.	 */
	void setDataBlock(int i_node, int i, int dblockNum);

	/*read the entire content of the dblockNum's and copy it into buf.	 */
	void readBlock(int dblockNum, char* buf);

	/*overwrite the content of block with number dblockNum with the newdata block.
	 * Assume newdata's length is equal to dataBlockSize.
	 */
	void writeBlock(int dblockNum, char* newdata);

	/*
	 *return the file size saved in the given i_node.
	 *This is the exact size of the file in bytes
	 *not the number of blocks used by this file.
	 */
	int getFileSize(int i_node);

	/* set the file size.	 */
	void setFileSize(int i_node, int newSize);
	SuperBlock kalEl;
private:

	int disk_file;
	list<inode> iNodes;
	int findNextFreeNode();
	void removeFromFreeNodeLst(int node_id);
	void clr_node_status(int freeInodeAdress,int blockSize);
	void inodeListInit();


};

#endif /* HARDDISK_H_ */
